﻿using FreeSql.DataAnnotations;
using MiniJob.Jobs;

namespace MiniJob.Entity;

/// <summary>
/// 任务信息
/// </summary>
[Index("idx_jobinfo_appid", "AppId")]
[Index("idx_jobinfo_name", "NextTriggerTime")]
public class JobInfo : Entity
{
    #region 任务基本信息
    /// <summary>
    /// 应用ID
    /// </summary>
    public Guid AppId { get; set; }

    /// <summary>
    /// 应用信息
    /// </summary>
    [Navigate(nameof(AppId))]
    public AppInfo? AppInfo { get; set; }

    /// <summary>
    /// 任务名称
    /// </summary>
    [Column(StringLength = 100)]
    public required string Name { get; set; }

    /// <summary>
    /// 任务描述
    /// </summary>
    [Column(StringLength = 255)]
    public string? Description { get; set; }

    /// <summary>
    /// 任务自带的参数
    /// </summary>
    [Column(StringLength = -2)]
    public string? JobParams { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enable { get; set; }

    /// <summary>
    /// 任务标签
    /// </summary>
    [Column(StringLength = 255)]
    public string? Tags { get; set; }
    #endregion

    #region 定时参数
    /// <summary>
    /// 时间表达式类型（CRON/API/FIX_RATE/Delayed）
    /// </summary>
    public TimeExpressionType TimeExpressionType { get; set; }

    /// <summary>
    /// 时间表达式，CRON/NULL/LONG/LONG（单位S）
    /// </summary>
    public string? TimeExpression { get; set; }
    #endregion

    #region 执行方式
    /// <summary>
    /// 执行类型，单机/广播/MR
    /// </summary>
    public ExecuteType ExecuteType { get; set; }

    /// <summary>
    /// 执行器类型，内建/Shell/Http/Sql
    /// </summary>
    public JobType JobType { get; set; }

    /// <summary>
    /// 任务类型信息
    /// </summary>
    [Column(StringLength = 255)]
    public string? JobFullName { get; set; }
    #endregion

    #region 运行时配置
    /// <summary>
    /// 最大同时运行任务数，默认 1
    /// </summary>
    public int MaxInstanceNum { get; set; }

    /// <summary>
    /// 并发度，同时执行某个任务的最大线程数量
    /// </summary>
    public int Concurrency { get; set; }

    /// <summary>
    /// 任务整体超时时间
    /// </summary>
    public long InstanceTimeLimit { get; set; }
    #endregion

    #region 重试配置
    /// <summary>
    /// 实例重试次数
    /// </summary>
    public int InstanceRetryNum { get; set; }

    /// <summary>
    /// 任务重试次数
    /// </summary>
    public int TaskRetryNum { get; set; }

    /// <summary>
    /// 下一次调度时间
    /// </summary>
    public long NextTriggerTime { get; set; }
    #endregion

    #region 集群配置
    /// <summary>
    /// 指定机器运行，空代表不限，非空则只会使用其中的机器运行（多值逗号分割）
    /// </summary>
    [Column(StringLength = 255)]
    public string? DesignatedWorkers { get; set; }

    /// <summary>
    /// 最大机器数量
    /// </summary>
    public int MaxWorkerCount { get; set; }

    /// <summary>
    /// 派发策略
    /// </summary>
    public DispatchStrategy DispatchStrategy { get; set; }

    /// <summary>
    /// 过期策略 忽略/补偿
    /// </summary>
    public MisfireStrategy MisfireStrategy { get; set; }
    #endregion
}
